const fs = require('fs');
const path = require('path');

/**
 * 加载文件夹下的所有文件
 * @param {*} fileAddr 
 * @returns 
 */
const scanFiles = fileAddr => {
  //解析需要遍历的文件夹，以F盘根目录为例
  // const filePath = path.resolve(fileAddr);
  const filePath = path.resolve(process.cwd(), './src' + fileAddr);

  //调用文件遍历方法
  // const routes = await fileDisplay(filePath);

  // console.log('filePath', filePath);

  const routes = [];
  const whiteList = ['index']; // 白名单
  const files = fs.readdirSync(filePath);
  // 遍历读取到的文件列表
  files.forEach(function (filename) {
    // console.log(filename);
    if (/\.js$/g.test(filename) && !whiteList.includes(filename.replace(/\.js$/g, ''))) {
      const route = require(path.join(filePath, filename));
      if (route instanceof Array) {
        routes.push(...route);
      } else {
        Object.assign(routes, route);
      }
      
      // console.log(filename, path.join(filePath, filename), routes);
    }
  });

  return routes;
};

module.exports = {
  scanFiles,
};
